﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.Manager;

namespace BoutiquePOS.UI.UserUI
{
    public partial class ShelfEntry : System.Web.UI.Page
    {
        private ShelfManager aManager = new ShelfManager();
        private Shelf aShelf = new Shelf();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                LoadGridview();
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void SaveCompany(object sender, EventArgs e)
        {
            try
            {
                aManager = new ShelfManager();
                aShelf.ShelfName = shelfNameTextBox.Value;
                string message = aManager.SaveShelf(aShelf);
                messageResult.InnerText = message;
                Clear();
            }
            catch (Exception exception)
            {
                messageResult.InnerText = "Company name can not be saved" + "\n" + exception.ToString();
            }
        }

        public void CloseButton(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

        public void ClearButton(object sender, EventArgs e)
        {
            Clear();
        }
        private void Clear()
        {
            shelfIdTextBox.Value = "";
            shelfNameTextBox.Value = "";
        }

        public void DeleteButton(object sender, EventArgs e)
        {
            try
            {
                int ShelfId = Convert.ToInt32(shelfIdTextBox.Value);
                ShelfManager shelfManager = new ShelfManager();
                string aa = shelfManager.DeleteShelf(ShelfId);
                messageResult.InnerText = aa;
            }
            catch (Exception)
            {
                messageResult.InnerText = "Delete Failed";
            }
        }

        private void LoadGridview()
        {
            try
            {
                List<Shelf> ashelf;
                ashelf = aManager.GetAllShelf();
                shelfGridView.DataSource = ashelf;
                shelfGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void LoadGridViewOnSaerch(int i, string shelfIdorName)
        {
            try
            {
                shelfGridView.DataSource = null;
                shelfGridView.DataBind();
                List<Shelf> shelfs;
                ShelfManager shelfManager = new ShelfManager();
                shelfs = shelfManager.GetAllShelfByIdOrName(i, shelfIdorName);
                shelfGridView.DataSource = shelfs;
                shelfGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void searchByIdButton_Click(object sender, EventArgs e)
        {
            int i = 0;
            LoadGridViewOnSaerch(i, searchIdTextBox.Value);
        }
        protected void searchByNameButton_Click(object sender, EventArgs e)
        {
            int i = 1;
            LoadGridViewOnSaerch(i, searchNameTextBox.Value);
        }
        protected void shelfGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            shelfGridView.PageIndex = e.NewPageIndex;
            LoadGridview();
        }


    }
}